package view;

import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Point;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.border.TitledBorder;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import common.loadData;
import model.complaintModel;
import dao.complaintdao;

public class panelComplaint extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel panelTitle = null;
	private JLabel lbtitle = null;
	private JButton ButtonAdd = null;
	private JScrollPane ScrollPaneComplaint = null;
	public static JTable TableComplaint = null;
	private JButton ButtonModify = null;
	private JButton ButtonDelete = null;
	public static int IdCom = -2;
	JFrame a;
	private JButton ButtonListCom_type = null;
	public panelComplaint(JFrame a) {
		super();
		this.a = a;
		initialize();
		
	}

	private void initialize() {
		this.setSize(735, 520);
		this.setLayout(null);
		this.add(getPanelTitle(), null);
		this.add(getScrollPaneComplaint(), null);
		this.add(getButtonAdd(), null);
		this.add(getButtonModify(), null);
		this.add(getButtonDelete(), null);
		this.add(getButtonListCom_type(), null);
	}

	private JPanel getPanelTitle() {
		if (panelTitle == null) {
			lbtitle = new JLabel();
			lbtitle.setLocation(new Point(0, 0));
			lbtitle.setFont(new Font("Dialog", Font.BOLD, 24));
			lbtitle.setForeground(new Color(0, 153, 153));
			lbtitle.setText("List Complaints");
			lbtitle.setSize(new Dimension(185, 34));
			panelTitle = new JPanel();
			panelTitle.setLayout(null);
			panelTitle.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(0, 0, 2, 0, Color.red), "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			panelTitle.setLocation(new Point(269, 15));
			panelTitle.setSize(new Dimension(186, 33));
			panelTitle.add(lbtitle, null);
		}
		return panelTitle;
	}

	private JButton getButtonAdd() {
		if (ButtonAdd == null) {
			ButtonAdd = new JButton();
			ButtonAdd.setText("Add");
			ButtonAdd.setLocation(new Point(353, 465));
	        ButtonAdd.setSize(new Dimension(100, 33));
			ButtonAdd.setIcon(new ImageIcon(getClass().getResource("/image/Create.png")));
			ButtonAdd.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					IdCom=-1;
					addComplaint com = new addComplaint();
					com.setVisible(true);
					a.dispose();
				}
			});
		}
		return ButtonAdd;
	}

	private JScrollPane getScrollPaneComplaint() {
		if (ScrollPaneComplaint == null) {
			ScrollPaneComplaint = new JScrollPane();
			ScrollPaneComplaint.setSize(new Dimension(646, 389));
			ScrollPaneComplaint.setLocation(new Point(44, 59));
			ScrollPaneComplaint.setViewportView(getTableComplaint());
		}
		return ScrollPaneComplaint;
	}

	private JTable getTableComplaint() {
			TableComplaint = new JTable(loadData.rowComplaint, loadData.columnComplaint);
			TableComplaint.setShowGrid(true);
			if(TableComplaint.getRowCount()==0)
			{
				JOptionPane.showMessageDialog(null, "No data for keyword '"+mainForm.search+"'!");
			}
		return TableComplaint;
	}

	private JButton getButtonModify() {
		if (ButtonModify == null) {
			ButtonModify = new JButton();
			ButtonModify.setHorizontalTextPosition(SwingConstants.RIGHT);
			ButtonModify.setIcon(new ImageIcon(getClass().getResource("/image/Modify.png")));
			ButtonModify.setText("Modify");
			ButtonModify.setLocation(new Point(471, 466));
			ButtonModify.setSize(new Dimension(100, 33));
			ButtonModify.setHorizontalAlignment(SwingConstants.LEFT);
			ButtonModify.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if((TableComplaint.getSelectedRow())>=0)
					{
					int row = TableComplaint.getSelectedRow();
					int column = 0;
					IdCom = Integer.parseInt(TableComplaint.getValueAt(row, column).toString());
						addComplaint com = new addComplaint();
						com.setTitle("Update Complaint");
						com.setVisible(true);
						a.dispose();
					}
					else
					{
						JOptionPane.showMessageDialog(null, "Choice row!");
					}
				}
			});
		}
		return ButtonModify;
	}

	private JButton getButtonDelete() {
		if (ButtonDelete == null) {
			ButtonDelete = new JButton();
			ButtonDelete.setHorizontalTextPosition(SwingConstants.RIGHT);
			ButtonDelete.setIcon(new ImageIcon(getClass().getResource("/image/Delete.png")));
			ButtonDelete.setText("Delete");
			ButtonDelete.setSize(new Dimension(100, 33));
			ButtonDelete.setLocation(new Point(589, 466));
			ButtonDelete.setHorizontalAlignment(SwingConstants.LEFT);
			if(login.idrole==1)
			{
				ButtonDelete.setEnabled(false);
			}
			ButtonDelete.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					int row = TableComplaint.getSelectedRow();
					int column = 0;
					IdCom = Integer.parseInt(TableComplaint.getValueAt(row, column).toString());
					complaintModel model = new complaintModel();
					model.setId(IdCom);
					int rsms = JOptionPane.showConfirmDialog(null, "Do you want to delete this user?");
					if(rsms==0)
					{
						int rs=complaintdao.deleteComplaint(model);
						if(rs==1)
						{
							JOptionPane.showMessageDialog(null,"Delete Complaint sucessful!");
							a.dispose();
							loadData.tab=5;
							(new mainForm()).setVisible(true);
						}
						else
						{
							JOptionPane.showMessageDialog(null,"Delete Complaint fail!");
						}
					}
				}
			});
		}
		return ButtonDelete;
	}
	
	private JButton getButtonListCom_type() {
		if (ButtonListCom_type == null) {
			ButtonListCom_type = new JButton();
			ButtonListCom_type.setLocation(new Point(181, 465));
			ButtonListCom_type.setText("Complaint Type");
			ButtonListCom_type.setIcon(new ImageIcon(getClass().getResource("/image/Listc.png")));
			ButtonListCom_type.setHorizontalAlignment(SwingConstants.LEFT);
			ButtonListCom_type.setHorizontalTextPosition(SwingConstants.RIGHT);
			ButtonListCom_type.setSize(new Dimension(154, 33));
			ButtonListCom_type.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					(new addComplaintType()).setVisible(true);
					
				}
			});
		}
		return ButtonListCom_type;
	}

}
